local INTTE = require "packages.internetMEME.record.baseme"
local U = require "packages.utility.utility"


-- 移除角色的所有技能并临时保存
-- 参数：
--   player: 要移除技能的玩家
--   room: 游戏房间
--   tag_name: 用于存储移除技能的标签名称，默认为"removed_skills"
--   mark_name: 用于设置标记的名称，默认为nil（不设置标记）
-- 返回值：
--   移除的技能列表
---@param player ServerPlayer
function INTTE.removeAllSkills(player, room, tag_name, mark_name)
  tag_name = tag_name or "removed_skills"

  -- 收集所有可见的非装备技能
  local skills = {}
  for _, skill_name in ipairs(player:getSkillNameList()) do
    local skill = Fk.skills[skill_name]
    if skill.visible and skill:isPlayerSkill(player) then
      table.insertIfNeed(skills, skill.name)
    end
  end

  -- 保存要移除的技能列表
  player.tag[tag_name] = skills

  -- 移除所有技能
  for _, skill_name in ipairs(skills) do
    room:handleAddLoseSkills(player, "-" .. skill_name, nil, false, true)
  end

  -- 如果提供了标记名称，设置标记
  if mark_name then
    room:setPlayerMark(player, mark_name, 1)
  end

  return skills
end

-- 恢复之前移除的技能
-- 参数：
--   player: 要恢复技能的玩家
--   room: 游戏房间
--   tag_name: 存储移除技能的标签名称，默认为"removed_skills"
--   mark_name: 要清除的标记名称，默认为nil（不清除标记）
-- 返回值：
--   恢复的技能列表
function INTTE.restoreRemovedSkills(player, room, tag_name, mark_name)
  tag_name = tag_name or "removed_skills"

  -- 获取保存的技能列表
  local skills = player.tag[tag_name] or {}

  -- 恢复所有技能
  for _, skill_name in ipairs(skills) do
    room:handleAddLoseSkills(player, skill_name, nil, false, true)
  end

  -- 清除标签
  player.tag[tag_name] = nil

  -- 如果提供了标记名称，清除标记
  if mark_name then
    room:setPlayerMark(player, mark_name, 0)
  end

  return skills
end

function INTTE.ChangeGeneral(player, old, new)
  local room = player.room
  if player.deputyGeneral == old or player.general == old then
    if player.deputyGeneral == old then
      player.deputyGeneral = new
      room:broadcastProperty(player, "deputyGeneral")
    elseif player.general == old then
      player.general = new
      room:broadcastProperty(player, "general")
    end
  else
    player.general = new
    room:broadcastProperty(player, "general")
  end
end

-- 移除角色的所有技能并临时保存
-- 参数：
--   player: 要移除技能的玩家
--   room: 游戏房间
--   tag_name: 用于存储移除技能的标签名称，默认为"removed_skills"
--   mark_name: 用于设置标记的名称，默认为nil（不设置标记）
-- 返回值：
--   移除的技能列表
function INTTE.removeREAllSkills(player, room, tag_name, mark_name)
  tag_name = tag_name or "removed_skills"

  -- 收集所有可见的非装备技能
  local skills = {}
  for _, skill in ipairs(player.player_skills) do
    if skill.visible and
        not skill.name:startsWith("#") and
        not skill.name:endsWith("&") and
        not skill:isEquipmentSkill(player) then
      table.insertIfNeed(skills, skill.name)
    end
  end

  -- 保存要移除的技能列表
  player.tag[tag_name] = skills

  -- 移除所有技能
  for _, skill_name in ipairs(skills) do
    room:handleAddLoseSkills(player, "-" .. skill_name, nil, false, true)
  end

  -- 如果提供了标记名称，设置标记
  if mark_name then
    room:setPlayerMark(player, mark_name, 1)
  end

  return skills
end

-- 恢复之前移除的技能
-- 参数：
--   player: 要恢复技能的玩家
--   room: 游戏房间
--   tag_name: 存储移除技能的标签名称，默认为"removed_skills"
--   mark_name: 要清除的标记名称，默认为nil（不清除标记）
-- 返回值：
--   恢复的技能列表
function INTTE.restoreRERemovedSkills(player, room, tag_name, mark_name)
  tag_name = tag_name or "removed_skills"

  -- 获取保存的技能列表
  local skills = player.tag[tag_name] or {}

  -- 恢复所有技能
  for _, skill_name in ipairs(skills) do
    room:handleAddLoseSkills(player, skill_name, nil, false, true)
  end

  -- 清除标签
  player.tag[tag_name] = nil

  -- 如果提供了标记名称，清除标记
  if mark_name then
    room:setPlayerMark(player, mark_name, 0)
  end

  return skills
end

---重新隐匿，玩家隐匿
---@param player ServerPlayer @ 目标角色
---@param kingdom string @ 隐匿势力
INTTE.EnterHidden = function(player, kingdom)
  local room = player.room
  room:sendLog({
    type = "#jbsEnterHidden",
    from = player.id,
  })
  local skills = "hidden_skill&"
  room:setPlayerMark(player, "__hidden_general", player.general)
  for _, s in ipairs(Fk.generals[player.general]:getSkillNameList(true)) do
    if player:hasSkill(s, true) then
      skills = skills .. "|-" .. s
    end
  end
  if player.deputyGeneral ~= "" then
    room:setPlayerMark(player, "__hidden_deputy", player.deputyGeneral)
    for _, s in ipairs(Fk.generals[player.deputyGeneral]:getSkillNameList(true)) do
      if player:hasSkill(s, true) then
        skills = skills .. "|-" .. s
      end
    end
  end
  player.general = "hiddenone"
  player.gender = General.Male
  room:broadcastProperty(player, "gender")
  if player.deputyGeneral ~= "" then
    player.deputyGeneral = ""
  end
  player.kingdom = kingdom or "jin"
  room:setPlayerMark(player, "__hidden_record",
    {
      maxHp = player.maxHp,
      hp = player.hp,
    })
  player.maxHp = 1
  player.hp = 1
  for _, property in ipairs({ "general", "deputyGeneral", "kingdom", "maxHp", "hp" }) do
    room:broadcastProperty(player, property)
  end
  room:handleAddLoseSkills(player, skills, nil, false, true)
end




---调整角色手牌数量
---@param player ServerPlayer 需要调整的角色
---@param num integer   需要调整至的数值
---@param prompt? string 调整数值不小于手牌时的弃牌提示
---@param skill Skill   本技能，一般填入self
---@return integer[] @ 因调整而弃置/摸到的牌ids（可能为空）
function INTTE.ChangeHandNum(player, num, prompt, skill)
  local room = player.room
  local difference = math.abs(player:getHandcardNum() - num)
  local cids = {}
  if player:getHandcardNum() > num then
    cids = room:askToDiscard(player, {
      min_num = difference,
      max_num = difference,
      cancelable = false,
      include_equip = false,
      prompt = prompt,
      skill_name = skill.name,
    })
  elseif player:getHandcardNum() < num then
    cids = player:drawCards(difference, skill.name)
  end
  return cids
end

return INTTE
